package com.sams.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sams.entity.Baseinfo;
import com.sams.entity.BaseinfoExample;
import com.sams.entity.School;
import com.sams.entity.SchoolExample;
import com.sams.entity.SchoolExample.Criteria;
import com.sams.mapper.BaseinfoMapper;
import com.sams.mapper.SchoolMapper;
import com.sams.service.SchoolService;
@Service
public class SchoolServiceImpl implements SchoolService{

	@Autowired
	private SchoolMapper mapper;
	@Autowired
	private BaseinfoMapper baseinfoMapper;
	public int add(School sc) throws Exception {
		sc.setStatus(1);
		// TODO Auto-generated method stub
		 if(!validate(sc.getName(),sc.getLevel())) {
			 throw new Exception(sc.getName()+"的学段为"+sc.getLevel()+"的数据当前时段已经存在，不能重复导入");
		 }
		return mapper.insert(sc);
	}

	public int delete(int id) {
		/*
		 * School school = mapper.selectByPrimaryKey(id); school.setStatus(0);// 逻辑删除
		 * //修改状态
		 */		
		return mapper.deleteByPrimaryKey(id);
	}
	
	
	/**
	 * 
	 */
	public PageInfo<School> listSchool(int currentPage,String name) {
		// 1 定义当前页  和 一页显示的行数 
		  PageHelper.startPage(currentPage, 20);  
		 
		
		  SchoolExample example = new SchoolExample();
		 Criteria createCriteria = example.createCriteria();
		 
		   //查询 没有删除的数据   不等于0 的数据
		// createCriteria.andStatusNotEqualTo(0);
		 if(name!=null && !"".equals(name)) {
		 createCriteria.andNameLike("%"+name+"%");
		 }
  		 
 		 // 2  调用查询语句
		List<School> list =   mapper.selectByExample(example);
		PageInfo<School> pageInfo = new PageInfo<School>(list);
		 
		return pageInfo;
	}

	public School findById(int id) {
	  
		School stu = mapper.selectByPrimaryKey(id);
		
		return stu;
	}

	public int update(School stu) {
		stu.setStatus(1);
		 
		return mapper.updateByPrimaryKey(stu);
	}
	public boolean importSchool(String path) {
		// TODO Auto-generated method stub
		return false;
	}

	public int updateStatus(Integer id, Integer status) {
		// TODO Auto-generated method stub
		School school = new School();
		school.setStatus(status);
		school.setSchid(id);
		return mapper.updateStatus(school);
	}

	public School selectByName(String name, Integer status) {
		  SchoolExample example = new SchoolExample();
			 Criteria createCriteria = example.createCriteria();
			 createCriteria.andNameEqualTo(name);
			 createCriteria.andStatusEqualTo(status);
			 List<School> list = mapper.selectByExample(example);
			 if(list!=null && list.size()>0) {
				 return list.get(0);
			 }
		return null;
	}

	//查询所有启用评审的学校
	public List<School> listAllSchool() {
		 
	     SchoolExample example = new SchoolExample();
		 Criteria createCriteria = example.createCriteria();
		 createCriteria.andStatusEqualTo(1);
		 List<School> list = mapper.selectByExample(example);
		return list;
	}
	/**
	 * 批量导出
	 * @throws Exception 
	 */
	public boolean insertBatch(List<School> schoolList) throws Exception {
		 for(School sc : schoolList) {
			 // 为了提高效率，一边导入一边验证（数据不合法，抛出异常）
			 if(!validateBaseInfo(sc.getName())) {
				 throw new Exception(sc.getName()+"在基础数据表中不存在，请先维护基础表");
			 }
			 if(!validate(sc.getName(),sc.getLevel())) {
				 throw new Exception(sc.getName()+"的学段为"+sc.getLevel()+"的数据当前时段已经存在，不能重复导入");
			 }
			
			 mapper.insert(sc);
		 }
		 //若没有异常，则导出成功 
		return true;
	}
	/**
	 * 系统根据 学校名称+学段+状态  判断唯一性
	 * @param schoolName
	 * @param level
	 * @return
	 */
	public boolean validate(String schoolName ,String level) {
		  SchoolExample example = new SchoolExample();
			 Criteria createCriteria = example.createCriteria();
			 createCriteria.andStatusEqualTo(1);
			 createCriteria.andNameEqualTo(schoolName);
			 createCriteria.andLevelEqualTo(level);
			 List<School> list = mapper.selectByExample(example);
			 if(list.size()>0) {
				 return false;
			 }
			 return true;
	}
	/**
	 * 验证学校名称，是否存在基础数据中
	 * @param schoolName
	 * @return
	 */
	public boolean validateBaseInfo(String schoolName) {
		BaseinfoExample example = new BaseinfoExample();
		com.sams.entity.BaseinfoExample.Criteria createCriteria = example.createCriteria();
		createCriteria.andNameEqualTo(schoolName);
	
			 List<Baseinfo> list = baseinfoMapper.selectByExample(example);
			 if(list.size()>0) {
				 return true;
			 }
			 return false;
	}
	/**
	 * 根据学校名称 和 学段 获取学校对象
	 * 用于导入数据时，反向获取学校id
	 * @param schoolName
	 * @param xueduan
	 * @return
	 */
	public School getSchoolByName(String schoolName ,String xueduan) {
		 SchoolExample example = new SchoolExample();
		 Criteria createCriteria = example.createCriteria();
		 createCriteria.andNameEqualTo(schoolName);
		 createCriteria.andLevelEqualTo(xueduan);
		 //启用状态的  1:启用  2：禁用
		 createCriteria.andStatusEqualTo(1);
		 List<School> list = mapper.selectByExample(example);
		 if(list.size()>0) {
			 return list.get(0);
		 }
		 return null;
	}
	/**
	 * 根据选择的乡镇id 查询它下面的所属学校
	 * @param zid
	 * @return
	 */
	public List<School> selectSchoolByZid(Integer zid) {
		// TODO Auto-generated method stub
		return mapper.selectSchoolByZid(zid);
	}
	


}
